


import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy import types
from sqlalchemy import *
from sqlalchemy.orm import *

engin=create_engine('sqlite:///2011-test.db',echo=True)
meta=MetaData()

def init_model(bind):
    global engine, Session
    engine = bind
    Session = orm.scoped_session(
            orm.sessionmaker(transactional=True, autoflush=True, bind=bind))
    orm.mapper(TIndex, index_table)
    orm.mapper(TCatalog, catalog_table)

index_table = sa.Table('t_index', meta,
                       sa.Column('id', types.Integer, primary_key=True, autoincrement=True),
                       sa.Column('userid', types.Integer),
                       sa.Column('catid', types.Integer, ForeignKey('t_catalog.id')),
                       sa.Column('type', types.Integer),
                       sa.Column('partnerid', types.Integer),
                       sa.Column('score', types.Integer))

catalog_table = sa.Table('t_catalog', meta,
                        sa.Column('id', types.Integer, primary_key=True, autoincrement=True),
                        sa.Column('catname', types.String(32)))
    
class Index(object):
    def __init__(self,userid,catid,type,partnerid,score):
        self.userid=userid
        self.catid=catid
        self.type=type
        self.partnerid=partnerid
        self.score=score
mapper(Index,index_table)
class Catalog(object):
    def __init__(self,catname):
        self.catname=catname
mapper(Catalog,catalog_table)

meta.create_all(engin)   

"""insql=users('yexiaoting')
print insql.username
s=Session(engin)
s.add(insql)
s.commit()
s.close()"""
